package demo07;

/**
 * @author JYH
 * @version 1.0
 * @date 2022/11/1 16:05
 */
public class TestDead {
    public static void main(String[] args) {
        Boy boy = new Boy();
        Girl girl = new Girl();
        boy.setName("男孩");
        girl.setName("女孩");

        boy.start();
        girl.start();
    }
}
class LockObject{
    public static Object lock1=new Object();
    public static Object lock2=new Object();
}
class Boy extends Thread{
    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (LockObject.lock1){
            System.out.println(Thread.currentThread().getName()+"获取一个筷子");
            synchronized (LockObject.lock2){
                System.out.println(Thread.currentThread().getName()+"获取另一个筷子");
                System.out.println(Thread.currentThread().getName()+"可以吃饭了");
            }
        }

    }
}
class Girl extends Thread{
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (LockObject.lock2){
            System.out.println(Thread.currentThread().getName()+"获取一个筷子");
            synchronized (LockObject.lock1){
                System.out.println(Thread.currentThread().getName()+"获取另一个筷子");
                System.out.println(Thread.currentThread().getName()+"可以吃饭了");
            }
        }

    }
}